package com.tanhua.admin.service;
import cn.hutool.core.collection.CollUtil;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service   /*1.2模块后台----》动态审核的Service*/
public class DynamicReviewService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private MovementApi movementApi;
    @DubboReference
    private VideoApi videoApi;
    @DubboReference
    private CommentApi commentApi;
    /* 动态管理-动态详情*/
    public MovementsVo findAllDynamic(String id) {
        Movement movement = movementApi.findById(id);
        if (movement==null ){
            throw new BusinessException("");
        }
        UserInfo userInfo = userInfoApi.findById(movement.getUserId());
        if (userInfo==null){
            throw new BusinessException("");
        }
        MovementsVo init = MovementsVo.init(userInfo, movement);
        return init;
    }
    /*动态通过*/
    public ResponseEntity examinationPassed(String[] pushIdlist) {
        for (String s : pushIdlist) {

            movementApi.update(s,1);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("messages","动态已通过");
        return ResponseEntity.ok(map);
    }

    /*动态不通过*/
    public ResponseEntity auditFailure(String[] pushIdlist) {
        for (String s : pushIdlist) {
            movementApi.update(s,2);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("messages","动态已失败");
        // String s = JSON.toJSONString(map);
        return ResponseEntity.ok(map);
    }




    //动态详情另一个接口
    public PageResult messagesPage(String messageID, Integer page, Integer pagesize) {
        //分页查询动态评论
        List<Comment> comments = commentApi.findComments(messageID, CommentType.COMMENT, page, pagesize);
        //如果没有评论,返回空结果
        if (CollUtil.isEmpty(comments)) {
            return new PageResult();
        }
        //获取到所有评论的userid
        List<Long> userId = CollUtil.getFieldValues(comments, "userId", Long.class);
        //查询详细信息
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userId, null);
        ArrayList<CommentVo> vos = new ArrayList<>();
        //整合数据
        for (Comment comment : comments) {
            UserInfo userInfo = userInfoMap.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0l, vos);
    }

}
